//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import { invoke } from "@tauri-apps/api/core";

export type USER_STATE = number;
export const USER_STATE_NORMAL = 0;     //正常状态
export const USER_STATE_FORBIDDEN = 1;  //冻结账户

export type BasicUserInfo = {
    display_name: string;
    logo_uri: string;
};

export type UserInfo = {
    user_id: string;
    user_name: string;
    basic_info: BasicUserInfo;
    create_time: number;
    update_time: number;
    user_state: USER_STATE;
};

export type LoginRequest = {
    user_name: string;
    passwd: string;
};

export type LoginResponse = {
    code: number;
    err_msg: string;
    session_id: string;
    user_info: UserInfo;
    user_secret: string;
};

export type LogoutRequest = {
    session_id: string;
};

export type LogoutResponse = {
    code: number;
    err_msg: string;
};

export type UpdateBasicRequest = {
    session_id: string;
    basic_info: BasicUserInfo;
};

export type UpdateBasicResponse = {
    code: number;
    err_msg: string;
};

export type ChangePasswdRequest = {
    session_id: string;
    old_passwd: string;
    new_passwd: string;
};

export type ChangePasswdResponse = {
    code: number;
    err_msg: string;
};

export type RegisterRequest = {
    user_name: string;
    passwd: string;
    reg_code: string;
    basic_info: BasicUserInfo;
};

export type RegisterResponse = {
    code: number;
    err_msg: string;
};


//登录
export async function login(request: LoginRequest): Promise<LoginResponse> {
    const cmd = 'user_api_cmds_login';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<LoginResponse>(cmd, {
        request,
    });
}

//登出
export async function logout(request: LogoutRequest): Promise<LogoutResponse> {
    const cmd = 'user_api_cmds_logout';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<LogoutResponse>(cmd, {
        request,
    });
}

//修改个人信息
export async function update_basic(request: UpdateBasicRequest): Promise<UpdateBasicResponse> {
    const cmd = 'user_api_cmds_update_basic';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateBasicResponse>(cmd, {
        request,
    });
}

//修改密码
export async function change_passwd(request: ChangePasswdRequest): Promise<ChangePasswdResponse> {
    const cmd = 'user_api_cmds_change_passwd';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ChangePasswdResponse>(cmd, {
        request,
    });
}

//注册
export async function register(request: RegisterRequest): Promise<RegisterResponse> {
    const cmd = 'user_api_cmds_register';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RegisterResponse>(cmd, {
        request,
    });
}

//获取用户id
export async function get_user_id(): Promise<string> {
    const cmd = 'user_api_cmds_get_user_id';
    return invoke<string>(cmd, {});
}

//获取用户session
export async function get_user_session(): Promise<string> {
    const cmd = 'user_api_cmds_get_session';
    return invoke<string>(cmd, {});
}